package duppentity.dto.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;
import utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/11/18 17:16
 */

public class UserInfoDtoUtil {

    public static List<UserInfoDto> getUserInfoDtoList(JsonObject data){
        List<UserInfoDto> userInfoDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        userInfoDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            UserInfoDto userInfoDto = convertUserInfoDto(listOne, data);
                            userInfoDtoList.add(userInfoDto);
                        }
                    }
                }
            }
        }
        return  userInfoDtoList;
    }

    private static UserInfoDto convertUserInfoDto(JSONObject listOne, JsonObject data){
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.id = listOne.getLongValue("id");
        userInfoDto.mobile = listOne.getString("mobile");
        userInfoDto.washCount = listOne.getIntValue("washCount");
        userInfoDto.amountRemain = Utils.getStringPrice(listOne.getIntValue("amountRemain"));
        userInfoDto.amountConsume = Utils.getStringPrice(listOne.getIntValue("amountConsume"));
        userInfoDto.regeditTime = listOne.getLongValue("regeditTime");
        userInfoDto.lastWashTime = listOne.getLongValue("lastWashTime");
        JSONArray userCarIds = listOne.getJSONArray("userCarIds");
        JSONArray userThirdUnionIds = listOne.getJSONArray("userThirdUnionIds");
        if (userCarIds != null && userCarIds.size() > 0) {
            for (Object userCarId : userCarIds) {
                JSONObject userCar = JSONObjectUtils.get(data, "user_car");
                JSONObject carLicensePlate = JSONObjectUtils.get(data, "car_license_plate");
                String carLicensePlateId = JSONObjectUtils.getString(userCar, userCarId.toString(), "carLicensePlateId");
                String license = JSONObjectUtils.getString(carLicensePlate, carLicensePlateId, "license");
                userInfoDto.licenses.add(license);
            }
        }
        if (userThirdUnionIds != null && userThirdUnionIds.size() > 0) {
            JSONObject userThirdUnionId = JSONObjectUtils.get(data, "user_third_union_id");
            userInfoDto.nickname = JSONObjectUtils.getString(userThirdUnionId, userThirdUnionIds.get(0).toString(), "nickname");
        }
        return  userInfoDto;
    }
}

